package cn.wei.youvierpc.server;

import io.vertx.core.Vertx;

public class VertxHttpServer implements HttpServer{
    @Override
    public void doStart(int port) {
        Vertx vertx = Vertx.vertx();

        //vert.x创建服务器
        io.vertx.core.http.HttpServer httpServer = vertx.createHttpServer();

        //处理请求
        /*httpServer.requestHandler(httpServerRequest -> {
            System.out.println("Received request:" + httpServerRequest.method() + " " + httpServerRequest.uri());

            //发送响应
            httpServerRequest.response()
                    .putHeader("content-type", "text/plain")
                    .end("Hello from Vert.x Http Server!");
        });*/
        httpServer.requestHandler(new HttpServerHandler());

        //启动服务器并监听指定端口
        httpServer.listen(port, httpServerAsyncResult -> {
            if (httpServerAsyncResult.succeeded()) {
                System.out.println("Server is now listening on the port " + port);
            } else {
                System.out.println("Fail to start server" + httpServerAsyncResult.cause());
            }
        });
    }
}
